//
//  IMTAboutCarViewController.m
//  INMOTION
//
//  Created by apple03 on 2017/4/17.
//  Copyright © 2017年 Inmotion. All rights reserved.
//

#import "IMTAboutCarViewController.h"
#import "KSLanguageManager.h"
#import "CarModel.h"
#import "IMTCANManager.h"
#import "IMTCANMessageFactory.h"
#import "IMTConfig.h"
#import "IMTUnitHelper.h"
#import "NSObject+HUD.h"

@interface IMTAboutCarViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *softwareVersionLabel;
@property (weak, nonatomic) IBOutlet UILabel *SNLabel;
@property (weak, nonatomic) IBOutlet UILabel *mileageLabel;
@property (weak, nonatomic) IBOutlet UILabel *remainingBatteryLabel;
@property (weak, nonatomic) IBOutlet UILabel *motorTemperatureLabel;

@property (weak, nonatomic) IBOutlet UILabel *softwareVersion;
@property (weak, nonatomic) IBOutlet UILabel *SN;
@property (weak, nonatomic) IBOutlet UILabel *mileage;
@property (weak, nonatomic) IBOutlet UILabel *remainingBattery;
@property (weak, nonatomic) IBOutlet UILabel *motorTemperature;

@property (weak, nonatomic) IBOutlet UILabel *activeTitleLB;
@property (weak, nonatomic) IBOutlet UILabel *activeDateLB;


@end

@implementation IMTAboutCarViewController
#pragma mark - BaseAPIDelegate

- (void)viewWillDisappear:(BOOL)animated {
    if (self.isMovingFromParentViewController) {
        [self dismissHUD];
    }
    [super viewWillDisappear:animated];
}

- (void)backAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"nav_back_white"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)] style:(UIBarButtonItemStylePlain) target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = left;

    [self localize];
    [self configData];
    
    UILongPressGestureRecognizer *toucP = [[UILongPressGestureRecognizer alloc]init];
    [toucP addTarget:self action:@selector(longPcLICK:)];
    toucP.minimumPressDuration = 1;
    toucP.delegate = self;
    [self.SN addGestureRecognizer:toucP];
    self.SN.userInteractionEnabled = YES;
}

- (void)longPcLICK:(UILongPressGestureRecognizer *)sender {
    UIAlertControllerStyle style = UIAlertControllerStyleActionSheet;
    if (IPAD) {
        style = UIAlertControllerStyleAlert;
    }
    UIAlertController *vc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:style];
    UIAlertAction *copyAction = [UIAlertAction actionWithTitle:IMTNewCarLocalizedString(@"copy") style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
        pasteBoard.string = self.SN.text;
    }];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:IMTNewCarLocalizedString(@"dialog_cancel") style:(UIAlertActionStyleCancel) handler:nil];
    [vc addAction:copyAction];
    [vc addAction:cancle];
    [self presentViewController:vc animated:YES completion:nil];
    
}

- (void)configData {
    if (IMTCarSeriesLorin(self.carModel.carType)) {
        self.softwareVersion.text = [self.carModel.l9_CarModel.carVersion stringByReplacingOccurrencesOfString:@"&" withString:@" "];
        self.motorTemperature.text = [NSString stringWithFormat:@"%.0f%@",[IMTUnitHelper temperatureForValue:self.carModel.temperature], IMTUnitHelper.temperatureUnit];
        
        self.activeDateLB.text = self.carModel.l9_CarModel.activationDate;
    }
    self.SN.text = self.carModel.sn;
    self.mileage.text = [NSString stringWithFormat:@"%.0f%@",[IMTUnitHelper mileageForValue:self.carModel.mileage], IMTUnitHelper.mileageUnit];
    self.remainingBattery.text = [NSString stringWithFormat:@"%.0f%%", self.carModel.battery];
    
//    self.activeDateLB.text = ;
}

- (void)localize {
    self.title = IMTNewCarLocalizedString(@"car_feature_about_car");
    self.softwareVersionLabel.text = IMTNewCarLocalizedString(@"car_info_fw_version");
    self.SNLabel.text = IMTNewCarLocalizedString(@"car_info_sn");
    self.mileageLabel.text = IMTNewCarLocalizedString(@"car_info_total_mileage");
    self.motorTemperatureLabel.text = IMTNewCarLocalizedString(@"car_info_temp_main_board");
    //"剩余电量"
    self.remainingBatteryLabel.text = IMTNewCarLocalizedString(@"car_info_battery_level");
    self.activeTitleLB.text = IMTNewCarLocalizedString(@"car_info_activate_date");
}


//- (void)getCarActiveDateWithNum:(int)num {
//    if (num >= 3) {
//        [self showErrorHUDWithText:@"获取激活时间失败"];
//        NSArray *timeStrAry = [self.carModel.l9_CarModel.activationDate componentsSeparatedByString:@" "];
//        self.activeDateLB.text = timeStrAry.count > 0  ? timeStrAry[0] : @"--";
//        if (self.carModel.l9_CarModel.activationDate) {
//            // 上传激活信息
////            [self activateCarWithData:self.carModel.l9_CarModel.activationDate];
//            self.activeDateLB.text = self.carModel.l9_CarModel.activationDate;
//        }
//        return;
//    }
//    num ++;
//
//    [self.carModel.l9_CarModel getCarDeviceActivationStatuSsuccessBlock:^{
//        NSArray *timeStrAry = [self.carModel.l9_CarModel.activationDate componentsSeparatedByString:@" "];
//        self.activeDateLB.text = timeStrAry.count > 0  ? timeStrAry[0] : @"--";
//
//        // 上传激活信息
//        [self activateCarWithData:self.carModel.l9_CarModel.activationDate];
//    } failBlock:^(NSError *error) {
//        if (error == nil) {
//            self.activeDateLB.text = @"--";
//            if (self.carModel.l9_CarModel.activationDate) {
//                // 上传激活信息
//                [self activateCarWithData:self.carModel.l9_CarModel.activationDate];
//            }
//        }else {
//            [self getCarActiveDateWithNum:num];
//        }
//    }];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

// MARK: - TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return  [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
